package com.ruoyi.web.controller.tool;

import com.google.common.collect.ImmutableMap;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ResultType;
import com.ruoyi.system.domain.Demo;
import com.ruoyi.system.service.ITestHBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("测试hbase")
@RestController
@RequestMapping("/test/hbase")
public class TestHBaseController extends BaseController {


    @Autowired
    private ITestHBaseService testHBaseService;

    /**
     * @param demo
     * @Descripton: 保存
     * @Author: Sorin
     * @Date: 2018/3/22
     */
    @ApiOperation("新增数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "content", value = "内容", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "avg", value = "值", dataType = "String", dataTypeClass = String.class),
    })
    @PostMapping("/save")
    public AjaxResult save(Demo demo) {
        try {
            testHBaseService.save(demo);
            return setResponseEntity(ResultType.SUCCESS.getDescription(), ResultType.SUCCESS.getCode(), null, true);
        } catch (Exception e) {
            e.printStackTrace();
            return setResponseEntity(ResultType.SERVER_ERROR.getDescription(), ResultType.SERVER_ERROR.getCode(), null, false);
        }
    }

    /**
     * @param id
     * @Descripton: 根据id查询
     * @Author: Sorin
     * @Date: 2018/3/22
     */
    @ApiOperation("根据用户获取信息")
    @GetMapping("/get/{id}")
    public AjaxResult getBean(@PathVariable String id) {
        try {
            List<Demo> apples = testHBaseService.getById(new Demo(), id);
            return setResponseEntity(ResultType.SUCCESS.getDescription(), ResultType.SUCCESS.getCode(), ImmutableMap.of("date", apples), true);
        } catch (Exception e) {
            e.printStackTrace();
            return setResponseEntity(ResultType.SERVER_ERROR.getDescription(), ResultType.SERVER_ERROR.getCode(), null, false);
        }
    }


    /**
     * @param reason
     * @param obj
     * @return ResultData
     * @author Sorin
     * @Description: json返回统一格式
     * @date 2017年9月6日 上午11:05:33
     */
    public AjaxResult setResponseEntity(String reason, String code, Object obj, boolean success) {
        AjaxResult body = new AjaxResult();
        body.put(AjaxResult.CODE_TAG, code);
        body.put(AjaxResult.DATA_TAG, obj);
        body.put(AjaxResult.MSG_TAG, reason);
        return body;
    }
}
